<?php
if (!defined('INWEB')) header("Location: ../index.php");
$a=getVar('a');
$id=getVar('id');
includeLang('octa');
switch($a)
{
    case 'add':
    if($_POST)
    {
        try
        {
            $code=getVar('code');
            $codename=getVar('code_name');
            $legal=getVar('legal');
            $trailer=getVar('trailer_border','bool');
            $s=getVar('s','bool');
            $g=getVar('g','bool');
            $r=getVar('r','bool');
            $k=getVar('k','bool');
            $z=getVar('z','bool');
            $sql->query("SELECT * FROM `octa_codes` WHERE `code`='$code';");
            if($sql->row_count)
            {
                msg($Lang['error'],$Lang['octa_code_exists'], 'error');
            }
            else
            {
                $sql->query("INSERT INTO `octa_codes` (`code`, `name`, `legal_sign`, `trailer_border`, `S`, `G`, `R`, `K`, `Z`) VALUES ('$code', '$codename', '$legal', '$trailer', '$s', '$g', '$r', '$k', '$z');");
                msg($Lang['success'],$Lang['octa_code_added'].'</br> <a href="?p='.$page.'">'.$Lang['back'].'</a>');
            }
        }
        catch(Exception $e)
        {
            msg($Lang['error'],$e->getMessage(),'error');
        }
    }
    else
    {
        echo '<form action="?p='.$page.'&a=add" method="post"><table border="1">';
        echo '<tr><th>'.$Lang['code'].'</th><td><input type="text" maxlength="3" name="code" /></td></tr>';
        echo '<tr><th>'.$Lang['code_name'].'</th><td><input type="text" name="code_name" /></td></tr>';
        echo '<tr><th>'.$Lang['legal'].'</th><td><input maxlength="1" type="text" name="legal" /></td></tr>';
        echo '<tr><th>'.$Lang['trailer_border'].'</th><td><input type="checkbox" name="trailer_border" /></td></tr>';
        echo '<tr><th>'.$Lang['s'].'</th><td><input type="checkbox" name="s" /></td></tr>';
        echo '<tr><th>'.$Lang['g'].'</th><td><input type="checkbox" name="g" /></td></tr>';
        echo '<tr><th>'.$Lang['r'].'</th><td><input type="checkbox" name="r" /></td></tr>';
        echo '<tr><th>'.$Lang['k'].'</th><td><input type="checkbox" name="k" /></td></tr>';
        echo '<tr><th>'.$Lang['z'].'</th><td><input type="checkbox" name="z" /></td></tr>';
        echo'</table><input type="submit" class="yt_button" value="'.$Lang['add'].'" /></form>';
    }
    break;
    case 'edit':
        if($_POST)
        {
            try
            {
                $code=getVar('code');
                $codename=getVar('code_name');
                $legal=getVar('legal');
                $trailer=getVar('trailer_border','bool');
                $s=getVar('s','bool');
                $g=getVar('g','bool');
                $r=getVar('r','bool');
                $k=getVar('k','bool');
                $z=getVar('z','bool');
                $sql->query("SELECT * FROM `octa_codes` WHERE `id`='$id';");
                $check=$sql->fetch_array();
                if($check['code']!=$code)
                {
                    $sql->query("SELECT * FROM `octa_codes` WHERE `code`='$code';");
                    if($sql->row_count)
                    {
                        msg($Lang['error'],$Lang['octa_code_exists'], 'error');
                    }
                    else
                    {
                        $sql->query("UPDATE `octa_codes` SET `code`='$code', `name`='$codename', `legal_sign`='$legal', `trailer_border`='$trailer', `S`='$s', `G`='$g', `R`='$r', `K`='$k', `Z`='$z' WHERE id='$id';");
                        msg($Lang['success'],$Lang['octa_code_updated'].'</br> <a href="?p='.$page.'">'.$Lang['back'].'</a>');
                    }
                }
                else
                {
                    $sql->query("UPDATE `octa_codes` SET `name`='$codename', `legal_sign`='$legal', `trailer_border`='$trailer', `S`='$s', `G`='$g', `R`='$r', `K`='$k', `Z`='$z' WHERE id='$id';");
                    msg($Lang['success'],$Lang['octa_code_updated'].'</br> <a href="?p='.$page.'">'.$Lang['back'].'</a>');
                }

            }
            catch(Exception $e)
            {
                msg($Lang['error'],$e->getMessage(),'error');
            }
        }
        else
        {
            $sql->query("SELECT * FROM `octa_codes` WHERE id='$id';");
            $oc=$sql->fetch_array();
            $tr=($oc['trailer_border'])?'checked="checked"':'';
            $s=($oc['S'])?'checked="checked"':'';
            $g=($oc['G'])?'checked="checked"':'';
            $r=($oc['R'])?'checked="checked"':'';
            $k=($oc['K'])?'checked="checked"':'';
            $z=($oc['Z'])?'checked="checked"':'';
            echo '<form action="?p='.$page.'&a=edit&id='.$id.'" method="post"><table border="1">';
            echo '<tr><th>'.$Lang['code'].'</th><td><input type="text" maxlength="3" name="code" value="'.$oc['code'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['code_name'].'</th><td><input type="text" name="code_name" value="'.$oc['name'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['legal'].'</th><td><input maxlength="1" type="text" name="legal" value="'.$oc['legal_sign'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['trailer_border'].'</th><td><input type="checkbox" name="trailer_border" '.$tr.' /></td></tr>';
            echo '<tr><th>'.$Lang['s'].'</th><td><input type="checkbox" name="s" '.$s.' /></td></tr>';
            echo '<tr><th>'.$Lang['g'].'</th><td><input type="checkbox" name="g" '.$g.' /></td></tr>';
            echo '<tr><th>'.$Lang['r'].'</th><td><input type="checkbox" name="r" '.$r.' /></td></tr>';
            echo '<tr><th>'.$Lang['k'].'</th><td><input type="checkbox" name="k" '.$k.' /></td></tr>';
            echo '<tr><th>'.$Lang['z'].'</th><td><input type="checkbox" name="z" '.$z.' /></td></tr>';
            echo'</table><input type="submit" class="yt_button" value="'.$Lang['edit'].'" /></form>';
        }
    break;
    case 'delete':
        if(isset($_GET['confirm']))
        {
            $sql->query("DELETE FROM `octa_codes` WHERE id='$id';");
            msg($Lang['success'],$Lang['octa_code_deleted']);
        }
        else
        {
            msg($Lang['warning'],$Lang['are_u_sure_delete_octa_code'].$id.' ?<br /> <a href="?p='.$page.'&a=delete&id='.$id.'&confirm">'.$Lang['yes'].'</a>/<a href="?p='.$page.'">'.$Lang['no'].'</a>','warning');
        }
    break;
    default:
        echo '<a href="?p='.$page.'&a=add"><img src="img/add.png" alt="'.$Lang['add'].'" title="'.$Lang['add'].'" /></a><table border="1">';
        echo '<tr><td>'.$Lang['code'].'</td><td>'.$Lang['code_name'].'</td><td>'.$Lang['legal'].'</td><td>'.$Lang['trailer_border'].'</td><td>'.$Lang['s'].'</td><td>'.$Lang['g'].'</td><td>'.$Lang['r'].'</td><td>'.$Lang['k'].'</td><td>'.$Lang['z'].'</td><td>'.$Lang['actions'].'</td></tr>';

        $ocs=$sql->query("SELECT * FROM `octa_codes`;");
        while($oc=$sql->fetch_array($ocs))
        {
            $tr=($oc['trailer_border'])?'checked="checked"':'';
            $s=($oc['S'])?'checked="checked"':'';
            $g=($oc['G'])?'checked="checked"':'';
            $r=($oc['R'])?'checked="checked"':'';
            $k=($oc['K'])?'checked="checked"':'';
            $z=($oc['Z'])?'checked="checked"':'';
            echo '<tr><td>'.$oc['code'].'</td><td>'.$oc['name'].'</td><td>'.$oc['legal_sign'].'</td><td><input type="checkbox"'.$tr.' /></td><td><input type="checkbox"'.$s.' /></td><td><input type="checkbox"'.$g.' /></td><td><input type="checkbox"'.$r.' /></td><td><input type="checkbox"'.$k.' /></td><td><input type="checkbox"'.$tr.' /></td><td><a href="?p='.$page.'&a=edit&id='.$oc['id'].'"><img src="img/edit.png" alt="'.$Lang['edit'].'" title="'.$Lang['edit'].'" /></a><a href="?p='.$page.'&a=delete&id='.$oc['id'].'"><img src="img/delete.png" alt="'.$Lang['delete'].'" title="'.$Lang['delete'].'" /></a></td></tr>';
        }
        echo '</table>';
    break;
}
?>